package com.dxb.sentinel;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName：com.dxb.sentinel.QuickStart <br>
 * Description：TODO <br>
 *
 * @author 戴晓波 <br>
 * Date 2021/11/25/025 10:27 <br>
 * @version v1.0 <br>
 */
public class QuickStart {
    private static void initFlowRules() {
        List<FlowRule> rules = new ArrayList<>();

        FlowRule rule = new FlowRule();
        rule.setResource("name");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setCount(9);

        rules.add(rule);

        FlowRuleManager.loadRules(rules);
    }

    public static void main(String[] args) throws InterruptedException {
        initFlowRules();
        while (true) {
            try (Entry entry = SphU.entry("name")) {
                System.out.println("Hello World!");
            } catch (BlockException e) {
                System.out.println("block!");
            }
            Thread.sleep(10);
        }
    }

}
